﻿using System;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
using System.Xml.Serialization;
using QuickShot.Shortcut;

namespace QuickShot.Preferences
{
    public sealed class Serializer
    {
        private static readonly string FILE_LOC = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\QuickShot\preferences.xml";
        private static readonly string DIR_PATH = FILE_LOC.Substring(0, FILE_LOC.LastIndexOf('\\'));

        public static void SaveSettings(UserPreferences preferences)
        {
            if (Directory.Exists(DIR_PATH) == false)
            {
                try
                {
                    Directory.CreateDirectory(DIR_PATH);
                }
                catch (Exception ex)
                {
                    if ((ex is IOException) || (ex is UnauthorizedAccessException))
                    {
                        MessageBox.Show(string.Format("Unable To Create Directory \"{0}\". Settings Will Not Save", DIR_PATH), "Whops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    MessageBox.Show(ex.Message, "A Handled Exception Occured");
                }
            }
            XmlSerializer serializer = new XmlSerializer(typeof(UserPreferences));
            try
            {
                using (TextWriter writer = new StreamWriter(FILE_LOC))
                    serializer.Serialize(writer, preferences);
            }
            catch (Exception ex)
            {
                if ((ex is IOException) || (ex is UnauthorizedAccessException))
                {
                    MessageBox.Show(string.Format("Unable To Create Access File Location \"{0}\". Settings Will Not Save", FILE_LOC), "Whops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                MessageBox.Show(ex.Message, "A Handled Exception Occured");
            }
        }

        public static UserPreferences LoadSettings()
        {
            try
            {
                UserPreferences preferences = null;
                XmlSerializer deserializer = new XmlSerializer(typeof(UserPreferences));
                using (TextReader reader = new StreamReader(FILE_LOC))
                    preferences = (UserPreferences)deserializer.Deserialize(reader);
                return preferences;
            }
            catch (Exception ex)
            {
                if (ex is InvalidOperationException || ex is FileNotFoundException || ex is DirectoryNotFoundException)
                {
                    if (ex is DirectoryNotFoundException) Directory.CreateDirectory(DIR_PATH);
                    UserPreferences p = new UserPreferences()
                    {
                        FullscreenHotkey = new Hotkey(Modifiers.Control, Keys.D1),
                        RegionHotkey = new Hotkey(Modifiers.Control, Keys.D2),
                        ActiveWindowHotkey = new Hotkey(Modifiers.Control, Keys.D3),
                        CapturePrimaryDisplay = true,
                        SaveAfterCapture = true,
                        AutomaticNaming = true,
                        SaveDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\QuickShot",
                        Format = ImageFormat.Png
                    };
                    Serializer.SaveSettings(p);
                    return p;
                }
                MessageBox.Show(ex.Message, "A Handled Exception Occured");
                return null;
            }
        }
    }
}